﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace AMing.Mvc4.Web
{
    public class HostnameHelperEX : HostnameHelper
    {
        /// <summary>
        /// 初始化HostnameHelperEX
        /// </summary>
        /// <param name="hostNameView"></param>
        /// <param name="request"></param>
        public HostnameHelperEX(HostNameView hostNameView, HttpRequestBase request)
            : base(request)
        {
            HostNameView = hostNameView;
        }

        private HostNameView HostNameView = null;

        /// <summary>
        /// 主机名跳转
        /// </summary>
        /// <param name="defaultActionResult"></param>
        /// <param name="hostnames"></param>
        /// <returns></returns>
        public override ActionResult HostnameJump(ActionResult defaultActionResult, params HostNameActionResult[] hostnames)
        {
            string hostname = this.Request.Url.Authority.ToLower();
            foreach (var item in hostnames)
            {
                string host = HostNameData.GetHostName(item.Key);
                if (host != null)
                {
                    if (hostname.Contains(host))
                    {
                        return View(item.Key, item.Value);
                    }
                }
            }

            return View(HostNameEnum._default, defaultActionResult);
        }


        private ActionResult View(HostNameEnum hostNameEnum, ActionResult actionResult)
        {
            Type type = actionResult.GetType();
            if (type.Equals(typeof(HostNameViewResult)))
            {
                HostNameViewResult temp = (HostNameViewResult)actionResult;
                if (temp != null)
                {
                    return HostNameView.View(hostNameEnum, temp.ViewPath);
                }
            }
            return actionResult;
        }
    }
}
